//
//  YXNewFeatureCollectionViewController.swift
//  YXweibo
//
//  Created by AS on 5/14/16.
//  Copyright © 2016 AS. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let ItemCount = 4
class YXNewFeatureCollectionViewController: UICollectionViewController {

    // MARK: - 属性
    /// layout
    private let layout = UICollectionViewFlowLayout()
   
    init()
    {
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
        

            }
    private func prepareCollectionView()
    {
        // Register cell classes
        self.collectionView!.registerClass(YXNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        // 设置layout参数
        // 让cell和屏幕一样大
        layout.itemSize = UIScreen.mainScreen().bounds.size
         // 水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
         // 取消弹簧效果
        collectionView?.bounces = false
        // 不要水平滚动条
       // collectionView?.showsHorizontalScrollIndicator = false
        //分页
        collectionView?.pagingEnabled = true
        
    }

   }


extension YXNewFeatureCollectionViewController
{
    // 某一组显示几个cell
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    // 每个cell长什么样

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 直接取cell
        // dequeueReusableCellWithReuseIdentifier: 取cell会先去缓存池中取cell,缓存池中取不到会使用注册的cell来创建
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)as! YXNewFeatureCell
        
        // 设置cell显示的index

        cell.index = indexPath.item
        //cell.backgroundColor = UIColor.orangeColor()
        
        return cell
        
        
    }
    
     /// scrollView停止滚动
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
         // 获取collectionView的偏移值,计算出当前停下来的是哪一页
        let index = Int(scrollView.contentOffset.x/scrollView.frame.width)
         // 判断是否是最后一页
        if index == ItemCount - 1 {
           
            // 获取cell,千万不要自己去创建cell,应该拿到正在显示的cell
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath)as! YXNewFeatureCell
            
            cell.startAnimation()
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}
